import java.util.Arrays;

public class MyArrayList implements Ilist{


    public int[] elem;

    public int usedSize;

    public static final int DEFALUT_CAPACITY=10;

    public MyArrayList(){
        this.elem=new int[DEFALUT_CAPACITY];
    }
    @Override
    public void add(int data) {

        //1.
        if(isFull()){
          //2.
            grow();

        }
        //3.
        this.elem[usedSize]=data;
        //4.
        this.usedSize++;

    }

    /**
     * 判满
     * @return
     */
    public Boolean isFull(){
        return this.usedSize==this.elem.length;

    }

    private void grow(){
        this.elem= Arrays.copyOf(this.elem,2*this.elem.length);

    }
    @Override
    public void add(int pos, int data) {
        if(isFull()){
            grow();
        }
        checkAddPos(pos,Constant.ADD_POS_ILLEGALITY);
        if(pos==usedSize){
            this.elem[pos]=data;
            usedSize++;
            return;
        }
        for(int i=usedSize;i>pos;i--){
            this.elem[i]=this.elem[i-1];
        }
        this.elem[pos]=data;
        this.usedSize++;

    }

    private void checkAddPos(int pos,String msg){
        if(pos<0||pos>usedSize){
            throw new checkAddPosException();
        }
    }
    @Override
    public boolean contains(int toFind) {
        for(int i=0;i<usedSize;i++){
            if(toFind==elem[i]){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i=0;i<usedSize;i++){
            if(toFind==elem[i]){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        checkEmpty(Constant.EMPTY_LIST);
        checkPos(pos,Constant.GET_POS_ILLEGALITY);
        return elem[pos];
    }

    private void checkPos(int pos,String msg){
        if(pos>=this.usedSize||pos<0){
            throw new PosIllegalityException(msg) ;
        }

    }
    private void checkEmpty(String msg){

        if(usedSize==0){
            throw new EmptyException(msg);
        }
    }


    @Override
    public void set(int pos, int value) {
        checkEmpty(Constant.EMPTY_LIST);
        checkPos(pos,Constant.SET_POS_ILLEGALITY);
        this.elem[pos]=value;

    }

    @Override
    public void remove(int toRemove) {

//        int i=0;
//        for(i=0;this.elem[i]!=toRemove;i++){
//            ;
//        }
//        for(int j=i;j<usedSize-1;j++){
//
//            this.elem[j]=this.elem[j+1];
//
//        }

        if(usedSize==0){
            throw new EmptyException();

        }
        int i=indexOf(toRemove);
        if(i==-1){
            System.out.println("没有找到要删除的数据！");
            return ;

        }
        for(int j=i;j<usedSize-1;j++){

            this.elem[j]=this.elem[j+1];

        }

            this.usedSize--;

        //如果是引用类型的数组，必须手动elem[usedsize]=null
    }

    @Override
    public int size() {

        return this.usedSize;
    }

    @Override
    public void clear() {
        usedSize=0;

        //如果数组是引用类型
//        for(int i=0;i<usedSize;i++){
//            elem[i]=null;
//
//        }
//        usedSize=0;
//


    }

    @Override
    public void display() {

    for(int i=0;i<usedSize;i++){
        System.out.print(elem[i]+" ");
    }
        System.out.println();
        }
}
